package com.xuyiwei.xblog.core.shiro.factory;

import com.xuyiwei.xblog.business.system.dao.MenuMapper;
import com.xuyiwei.xblog.business.system.dao.UserMapper;
import com.xuyiwei.xblog.business.system.model.User;
import com.xuyiwei.xblog.core.base.constant.enums.UserStatusEnum;
import com.xuyiwei.xblog.core.common.constant.factory.ConstantFactory;
import com.xuyiwei.xblog.core.shiro.ShiroUser;
import com.xuyiwei.xblog.core.util.Convert;
import com.xuyiwei.xblog.core.util.SpringContextHolder;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@DependsOn("springContextHolder")
@Transactional(readOnly = true)
public class ShiroFactroy implements IShiro {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    public static IShiro me() {
        return SpringContextHolder.getBean(IShiro.class);
    }

    @Override
    public User user(String account) {

        User user = userMapper.getByAccount(account);

        // 账号不存在
        if (null == user) {
            throw new CredentialsException();
        }
        // 账号被冻结
        if (user.getStatus() != UserStatusEnum.USE.getKey()) {
            throw new LockedAccountException();
        }
        return user;
    }

    @Override
    public ShiroUser shiroUser(User user) {
        ShiroUser shiroUser = new ShiroUser();
        BeanUtils.copyProperties(user,shiroUser);
//        shiroUser.setId(user.getId());
//        shiroUser.setAccount(user.getAccount());
//        shiroUser.setDeptId(user.getDeptid());
//        shiroUser.setName(user.getName());

        shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));


        Long[] roleArray = Convert.toLongArray(user.getRoleid());
        List<Long> roleList = new ArrayList<Long>();
        List<String> roleNameList = new ArrayList<String>();
        for (Long roleId : roleArray) {
            roleList.add(roleId);
            roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
        }
        shiroUser.setRoleList(roleList);
        shiroUser.setRoleNames(roleNameList);

        return shiroUser;
    }

    @Override
    public List<String> findPermissionsByRoleId(Long roleId) {
        return menuMapper.getResUrlsByRoleId(roleId);
    }

    @Override
    public String findRoleNameByRoleId(Long roleId) {
        return ConstantFactory.me().getSingleRoleTip(roleId);
    }

    @Override
    public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
//        String credentials = user.getPassword();
//
//        // 密码加盐处理
//        String source = user.getSalt();
//        ByteSource credentialsSalt = new Md5Hash(source);
//        return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
        String source = user.getSalt();
        ByteSource credentialsSalt = new Md5Hash(source);
        if (user.getStatus() != null && UserStatusEnum.FREEZED.getKey().equals(user.getStatus())) {
            throw new LockedAccountException("帐号已被锁定，禁止登录！");
        }

        // principal参数使用用户Id，方便动态刷新用户权限
        return new SimpleAuthenticationInfo(
                shiroUser,
                user.getPassword(),
                credentialsSalt,
                realmName
        );
    }

}
